package com.demo.exception;

import java.io.IOException;
import java.sql.SQLException;

import javax.transaction.SystemException;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;

import com.demo.action.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

 

@SuppressWarnings("serial")

/**
 * 前端友情异常提示处理，只在后台展示详细的异常信息，前端不展示
 */
public class ExceptionInterceptor extends AbstractInterceptor {

 

    @SuppressWarnings("unchecked")
    public String intercept(ActionInvocation actionInvocation) throws Exception {
    	Logger log = Logger.getLogger(LoginAction.class);	
       
       String result = ""; 
       try { 
           result = actionInvocation.invoke();  
       } catch (DataAccessException ex) {  
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("数据库操作失败！");  

       } catch (NullPointerException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("空指针，调用了未经初始化或者是不存在的对象！");

       } catch (IOException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("IO读写异常！");

       } catch (ClassNotFoundException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("指定的类不存在！");

       } catch (ArithmeticException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("数学运算异常！");

       } catch (ArrayIndexOutOfBoundsException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("数组下标越界！");

       } catch (IllegalArgumentException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("调用方法的参数错误！");

       } catch (ClassCastException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("类型强制转换错误！");

       } catch (SecurityException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("违背安全原则异常！");

       } catch (SQLException ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("操作数据库异常！");

       } catch (NoSuchMethodError ex) {
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("调用了未定义的方法！");

       } catch (InternalError ex) { 
    	   log.error(getExceptionDetail(ex));
           throw new SystemException("Java虚拟机发生了内部错误！");

       } catch (Exception ex) { 
    	   log.error(getExceptionDetail(ex));
    	   throw new SystemException("程序内部错误，操作失败！"); 
       
       } 
       return result;  
    }
    
    /**
     * 获取异常出错详细信息
     * @param objectException 异常类对象
     * @return
     */
    public String getExceptionDetail(Object objectException){
    	StringBuffer msg = new StringBuffer("null"); 
		Exception exception = (Exception)objectException; 
		if (exception != null) {
			msg = new StringBuffer(""); 
			String message = exception.toString(); 
			int length = exception.getStackTrace().length; 
			if (length > 0) { 
				msg.append(message + "\n"); 
				for (int i = 0; i < length; i++) { 
					msg.append("\t" + exception.getStackTrace()[i] + "\n"); 
				}
			} else { 
				msg.append(message);
			} 
		}
		
		return  msg.toString();
	}
    
}